package com.mercycrazy.recruit.entity;

import com.mercycrazy.recruit.converter.RoleConverter;
import com.mercycrazy.recruit.converter.SexConverter;
import com.mercycrazy.recruit.converter.ShowWhichNameConverter;
import com.mercycrazy.recruit.enums.Role;
import com.mercycrazy.recruit.enums.Sex;
import com.mercycrazy.recruit.enums.ShowWhichName;
import com.mercycrazy.recruit.user.UserInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 用户表实体类定义
 *
 * @author <a href="mailto:1443424326@qq.com">mercycrazy</a>
 * @since 2023-01-10
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "t_user")
public class User implements Serializable {

    /**
     * 自增主键
     */
    @Id
    @GeneratedValue(generator = "SnowIdGenerator")
    @GenericGenerator(name = "SnowIdGenerator", strategy = "com.mercycrazy.recruit.support.jpa.SnowIdGenerator")
    @Column(name = "id", nullable = false)
    private String id;

    /**
     * 手机号
     */
    @Column(name = "mobile", nullable = false)
    private String mobile;

    /**
     * 昵称
     */
    @Column(name = "nickname", nullable = false)
    private String nickname;

    /**
     * 真实姓名
     */
    @Column(name = "real_name", nullable = false)
    private String realName;

    /**
     * 对外展示名, 1: 真实姓名, 2: 昵称
     */
    @Column(name = "show_which_name", nullable = false)
    @Convert(converter = ShowWhichNameConverter.class)
    private ShowWhichName showWhichName;

    /**
     * 性别，1:男 0:女 2:保密
     */
    @Column(name = "sex", nullable = false)
    @Convert(converter = SexConverter.class)
    private Sex sex;

    /**
     * 用户头像
     */
    @Column(name = "face", nullable = false)
    private String face;

    /**
     * 邮箱
     */
    @Column(name = "email", nullable = false)
    private String email;

    /**
     * 生日
     */
    @Column(name = "birthday", nullable = false)
    private LocalDate birthday;

    /**
     * 国家
     */
    @Column(name = "country", nullable = false)
    private String country;

    /**
     * 省份
     */
    @Column(name = "province", nullable = false)
    private String province;

    /**
     * 城市
     */
    @Column(name = "city", nullable = false)
    private String city;

    /**
     * 区县
     */
    @Column(name = "district", nullable = false)
    private String district;

    /**
     * 介绍
     */
    @Column(name = "description", nullable = false)
    private String description;

    /**
     * 我参加工作的时间
     */
    @Column(name = "start_work_date")
    private LocalDate startWorkDate;

    /**
     * 我当前职位/职务
     */
    @Column(name = "position", nullable = false)
    private String position;

    /**
     * 身份角色, 1: 求职者, 2: 求职者可以切换为HR进行招聘, 同时拥有两个身份
     */
    @Column(name = "role", nullable = false)
    @Convert(converter = RoleConverter.class)
    private Role role;

    /**
     * 成为 HR 后, 认证的(绑定的)公司主键 id
     */
    @Column(name = "hr_in_which_company_id")
    private String hrInWhichCompanyId;

    /**
     * HR 个性签名
     */
    @Column(name = "hr_signature")
    private String hrSignature;

    /**
     * HR 个性化标签
     */
    @Column(name = "hr_tags")
    private String hrTags;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 将 UserInfo 转成实体对象
     */
    public static User to(UserInfo userInfo) {

        User user = new User();

        user.setId(userInfo.getId());
        user.setMobile(userInfo.getMobile());
        user.setNickname(userInfo.getNickname());
        user.setRealName(userInfo.getRealName());
        user.setShowWhichName(ShowWhichName.of(userInfo.getShowWhichName()));
        user.setSex(Sex.of(userInfo.getSex()));
        user.setFace(userInfo.getFace());
        user.setEmail(userInfo.getEmail());
        user.setBirthday(userInfo.getBirthday());
        user.setCountry(userInfo.getCountry());
        user.setProvince(userInfo.getProvince());
        user.setCity(userInfo.getCity());
        user.setDistrict(userInfo.getDistrict());
        user.setDescription(userInfo.getDescription());
        user.setStartWorkDate(userInfo.getStartWorkDate());
        user.setPosition(userInfo.getPosition());
        user.setRole(Role.of(userInfo.getRole()));
        user.setHrInWhichCompanyId(userInfo.getHrInWhichCompanyId());
        user.setHrSignature(userInfo.getHrSignature());
        user.setHrTags(userInfo.getHrTags());
        user.setCreateTime(userInfo.getCreateTime());
        user.setUpdateTime(userInfo.getUpdateTime());

        return user;
    }

    /**
     * 将实体对象转成 UserInfo 对象
     */
    public UserInfo toUserInfo() {

        UserInfo userInfo = new UserInfo();

        userInfo.setId(this.id);
        userInfo.setMobile(this.mobile);
        userInfo.setNickname(this.nickname);
        userInfo.setRealName(this.realName);
        userInfo.setShowWhichName(this.showWhichName.getType());
        userInfo.setSex(this.sex.getType());
        userInfo.setFace(this.face);
        userInfo.setEmail(this.email);
        userInfo.setBirthday(this.birthday);
        userInfo.setCountry(this.country);
        userInfo.setProvince(this.province);
        userInfo.setCity(this.city);
        userInfo.setDistrict(this.district);
        userInfo.setDescription(this.description);
        userInfo.setStartWorkDate(this.startWorkDate);
        userInfo.setPosition(this.position);
        userInfo.setRole(this.role.getType());
        userInfo.setHrInWhichCompanyId(this.hrInWhichCompanyId);
        userInfo.setHrSignature(this.hrSignature);
        userInfo.setHrTags(this.hrTags);
        userInfo.setCreateTime(this.createTime);
        userInfo.setUpdateTime(this.updateTime);

        return userInfo;
    }
}